# 文件验证工具
# utils/file_checker.py

import os
from excelFileFormat.config import REQUIRED_FIELDS, ALLOWED_EXTENSIONS

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def validate_required_fields(sheet_name, df):
    missing = []
    expected_fields = REQUIRED_FIELDS.get(sheet_name, [])
    actual_fields = df.columns.tolist()

    for field in expected_fields:
        if field not in actual_fields:
            missing.append(field)
    return missing

def exact_field_match(sheet_name, df):
    expected = REQUIRED_FIELDS.get(sheet_name, [])
    actual = list(df.columns)
    if len(expected) != len(actual):
        return False, f"{sheet_name} 表字段数量不一致，应为 {len(expected)} 个，实际为 {len(actual)} 个"
    for i, (e, a) in enumerate(zip(expected, actual)):
        if e != a:
            return False, f"{sheet_name} 表第 {i+1} 个字段应为 '{e}'，实际为 '{a}'"
    return True, ""
